<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';
include("FCKeditor/fckeditor.php") ;


//Resume Info
$id = $_GET['id'] + 0;
$sql = "SELECT * FROM pa_resume WHERE re_id = '".$id."' AND re_uid <> '".$_SESSION['uid']."'";
$resumeInfo = $DB->fetch_one_array($sql);

//Not A Recuiter
if($_SESSION[usertype] != "recruiter" || empty($resumeInfo)){
   echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1011\">";
   exit;	
}

//recruiter Info
if($resumeInfo['re_usertype'] == 'recruiter'){
	$sql = "SELECT rc_uname,rc_copname,rc_fname, rc_lname,rc_email FROM pa_recuiter WHERE rc_id = '".$resumeInfo['re_uid']."' ";
	$recInfo = $DB->fetch_one_array($sql);
}

//jquery
$smarty->assign('jquery',1);
$smarty->assign('tokeninput',1);

$smarty->display('header.tpl');
$smarty->assign("date",date("m-d-Y"));
$smarty->assign("from",$_SESSION['realname']."[".$_SESSION['useremail']."]");

if($_GET['action']=="send" || $_GET['action'] == "resend" ){
	  
	  $err = '';
	  if($_GET['action'] == "send"){//send
		  if(trim($_POST['e_subject'])==''){
			  $err .= "Subject is required<br>";
		  }		  
	  }else{//resend
		 $id = $_GET['id']+0; 
		 if($id <= 1) {
			echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
			exit;
		 }
		 $sql = "SELECT * FROM pa_email WHERE e_sentrcid = '".$_SESSION['uid']."' AND e_id = '".$id."' ";
		 $eInfo = $DB->fetch_one_array($sql);
	  }
	  if($err == ''){
			if($resumeInfo['re_usertype'] == 'recruiter') {
				$to_email = $recInfo['rc_email'];
			}
			if($resumeInfo['re_usertype'] == 'candidate') {
				$to_email = $resumeInfo['re_email'];
			}	

			//$to_name = $recInfo['rc_fname'] ." ".$recInfo['rc_lname'];
			if($resumeInfo['re_usertype'] == 'recruiter') {
				$to_name = $recInfo['rc_uname'];
			}
			if($resumeInfo['re_usertype'] == 'candidate') {
				$to_name = $resumeInfo['re_uname'];
			}	
			
			$content = $_POST['content'] ;
	  }

	  if($err == ''){
			if(trim($_POST['preview']) == "Preview"){//Preview
				$emailInfo['e_subject'] = $_POST['e_subject'];
				$emailInfo['e_fromemail'] = $_SESSION['useremail'];
				$emailInfo['e_content'] = $content;
			
				
				//$emailInfo['e_toemail'] = $to_email."(".$to_name.")";
				$emailInfo['e_toemail'] = $to_name;
					
				if($jobInfo == ''){$jobInfo = 'N/A';}
				$emailInfo['attachJobs'] = $jobInfo;
				$emailInfo['date'] = date("m-d-Y");
				$emailInfo['realname'] = $_SESSION['realname'];
				$smarty->assign("s",$emailInfo);

				$smarty->display('allleft.tpl');
				$smarty->display('previewsendmessage.tpl');
				$smarty->display('footer.tpl');
				exit;
			}
			
			if($resumeInfo['re_usertype'] == 'recruiter') {
				$etype = 1;
			}
			if($resumeInfo['re_usertype'] == 'candidate') {
				$etype = 2;
			}	

		    if($_GET['action'] == "send"){//send
				//Save Email
				$sql = " INSERT INTO pa_email (
					`e_sentrcid`,
					`e_receivedrcid`,
					`e_sendertype`,
					`e_receivertype`,
					`e_type`,
					`e_fromemail`,
					`e_fromname`,
					`e_toemail`,
					`e_toname`,
					`e_subject`,
					`e_content`,
					`e_created`							
				) VALUES (
					 '".$_SESSION['uid']."',
					 '".$resumeInfo['re_uid']."',
					 'recruiter',
					 '".$resumeInfo['re_usertype']."',
					 '".$etype."',
					 '".$_SESSION['useremail']."',
					 '".$_SESSION['realname']."',
					 '".$to_email."',
					 '".$to_name."',
					 '".mysql_real_escape_string($_POST['e_subject'])."',
					 '".mysql_real_escape_string($_POST['content'])."',
					'".time()."'
				)";
				$DB->query($sql);
			}

			

			//Send Email
			require_once("./include/class.phpmailer.php");
			
			
			$mail=new PHPMailer();

			$mail->IsMail();
			$mail->From     = $_SESSION['useremail'];
			$mail->FromName = $_SESSION['realname'];
			$mail->AddAddress($to_email,$to_name);
			

			$mail->IsHTML(true);
			$mail->Subject = stripslashes($_POST['e_subject']);
			$mail->Body = "Message Forwarded from <a href='www.enetrecruiter.com'>enetRecruiter.com</a> <br><br>".
			stripslashes($content);

			$mail->CharSet = 'UTF-8';

			$mail->Send();
			$err  = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"2;URL=myemails.php\">";
	  }
}

//all my jobs
$sql = "SELECT jo_id,jo_title,jo_city,jo_state FROM pa_job WHERE jo_uid = '".$_SESSION['uid']."' AND jo_utype='recruiter' AND jo_status <> '3' ORDER BY jo_id DESC";
$tp_jobs = $DB->fetch_all($sql);
if(!empty($tp_jobs)){
	foreach($tp_jobs as $job){
		$jobs[] = '{"name":"=>'.$job['jo_title'].' ('.$job['jo_id'].')","id":"'.$job['jo_id'].'"}';
	}
}
$jobOptions = @implode(",",$jobs);
$jobOptions = str_replace("++","+",$jobOptions);

//FCKeditor
$oFCKeditor = new FCKeditor("content") ;
$oFCKeditor->BasePath	= "FCKeditor/" ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = 500;
$oFCKeditor->Height =300;
$oFCKeditor->ToolbarSet = "Basic";
//$oFCKeditor->Value = "<p><b>Dear  :</b><p><br /><br /><br /><br /><br /><br /><br /><br /><p><b>Best Regards<b></p><p><b>".$_SESSION['realname']."<b></p><p><b>".$_SESSION['phone']."<b></p>";
$oFCKeditor->Value = "";

$smarty->assign("fckcontent",$oFCKeditor->CreateHtml());


if($err!=''){
$smarty->assign("err",$err);
}
//$smarty->assign("myjobs",$jobOptions);

if($resumeInfo['re_usertype'] == 'recruiter') {
	$pfsubject = "Interested in your candidate: ".$resumeInfo['re_nickname'];
}
if($resumeInfo['re_usertype'] == 'candidate') {
	$pfsubject = "Interested in your resume: ".$resumeInfo['re_fname']." ".$resumeInfo['re_lname'];
}

if($resumeInfo['re_usertype'] == 'recruiter') {
	$pfsendto = "<b>".$recInfo['rc_fname']. " ". $recInfo['rc_lname']."</b>";
}
if($resumeInfo['re_usertype'] == 'candidate') {
	$pfsendto = "<b>".$resumeInfo['re_fname']. " ". $resumeInfo['re_lname']."[". $resumeInfo['re_email'] ."]</b>";
}

$smarty->assign("pfsubject",$pfsubject);
$smarty->assign("pfsendto",$pfsendto);
$smarty->assign("resumeid",$id);
$smarty->display('allleft.tpl');
$smarty->display('sendmessage.tpl');
$smarty->display('footer.tpl');


function attachContents($jobIds){
	global $state,$country,$DB,$_SESSION;
	$ids = @explode(",",$jobIds);
	if($ids[0] != ''){
		$str .= "<table width='520'>
					<tr><td colspan='2'><b>Job Details<br></td></tr>";
		$str .= "	<tr><td colspan='2'><br>-----------------------------<br></td></tr>";
		foreach($ids as $id){
			$sql = "SELECT jo_title, jo_copname, jo_copurl, jo_description, jo_address, jo_city, jo_state, jo_country FROM pa_job WHERE jo_uid='".$_SESSION['uid']."' AND jo_utype='recruiter' AND jo_id = '".$id."'";
			$job = $DB->fetch_one_array($sql);
			$str .= "<tr><td width='25%' valign='top'><b>Req#:</b></td><td valign='top'>".$id."</td></tr>";
			$str .= "<tr><td width='25%' valign='top'><b>Title:</b></td><td valign='top'>".$job['jo_title']."</td></tr>";
			$str .= "<tr><td valign='top'><b>Location:</b></td><td valign='top'>".$job['jo_address']."<br />".$job['jo_city'].",".$state[$job['jo_state']].",".$country[$job['jo_country']]."</td></tr>";
			if(preg_match("/Please select or click on a PASTE option above/i",$job['jo_description'])){
				$job['jo_description'] = "No Descriptions";
			}
			$str .= "<tr><td valign='top'><b>Description:</b></td><td valign='top'>".$job['jo_description']."</td></tr>";

			$str .= "	<tr><td colspan='2'><br>-----------------------------<br></td></tr>";
		}
		$str .= "</table>";
	}
	return $str;
}


?>